//
//  SocialViewContactCell.swift
//  iModern
//
//  Created by Ralf on 7/8/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class SocialViewContactCell: TableViewCell
{
    let __avatar_image_view_tag = 10
    let __follow_button_tag     = 30
    let __alias_label_tag       = 20

    var avatarImageView: UIImageView!
    var aliasLabel:      UILabel!
    var followButton:    UIButton!

    var userName: String!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        initAvatarImageView()
        initAliasLabel()
        initFollowButton()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func initAvatarImageView()
    {
        if avatarImageView == nil
        {
            let width = UIScreen.mainScreen().bounds.width / 5 - 20
            let frame = CGRect(x: 10, y: 10, width: width, height: width)
            avatarImageView = UIImageView(frame: frame)
            avatarImageView!.layer.cornerRadius = width / 2
            avatarImageView!.layer.masksToBounds = true
            avatarImageView!.layer.borderColor = UIColor.blackColor().CGColor
            avatarImageView!.layer.borderWidth = 1.0
            avatarImageView!.tag = __avatar_image_view_tag
            self.addSubview(avatarImageView!)
        }
    }

    func initAliasLabel()
    {
        if aliasLabel == nil
        {
            let x      = avatarImageView.frame.origin.x + avatarImageView.frame.size.width + 10
            let height = UIScreen.mainScreen().bounds.width / 5 - 20
            let frame  = CGRect(x: x, y: 10, width: 120, height: height)
            aliasLabel = UILabel(frame: frame)
            aliasLabel!.font = UIFont.systemFontOfSize(14)
            aliasLabel!.tag = __alias_label_tag
            self.addSubview(aliasLabel!)
        }
    }

    func initFollowButton()
    {
        if followButton == nil
        {
            let x      = self.screen.size.width - 125
            let height = UIScreen.mainScreen().bounds.width / 5 / 3
            let frame  = CGRect(x: x, y: height, width: 120, height: height)
            followButton = UIButton(frame: frame)
            followButton!.contentMode = .Left
            followButton!.titleLabel?.font = UIFont.systemFontOfSize(12)
            followButton!.titleLabel!.shadowColor = UIColor.darkGrayColor()
            followButton!.titleLabel!.shadowOffset = CGSizeMake(1, 1)
            followButton!.tag = __follow_button_tag
            followButton!.imageView?.contentMode = .ScaleAspectFit

            followButton!.setTitle("已关注", forState: .Selected)
            followButton!.setImage(UIImage(named: "Followed-60.png"), forState: .Selected)
            followButton!.setTitle("未关注", forState: .Normal)
            followButton!.setImage(UIImage(named: "Unfollowed-60.png"), forState: .Normal)
            followButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
            followButton.setTitleColor(UIColor.blackColor(), forState: .Selected)

            followButton.addTarget(self, action: "addFollowing:", forControlEvents: .TouchUpInside)

            self.addSubview(followButton!)
        }
    }

    func setData(data: [String:AnyObject])
    {
        let contact = data["contact"] as! [String:AnyObject]
        aliasLabel.text = contact["alias"] as? String
        userName = contact["userName"] as! String

        let thumbnailURL = contact["thumbnailURL"] as? String
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            avatarImageView.load(url, placeholder: nil)
        }
        else
        {
            avatarImageView.image = UIImage(named: "Avatar-Default")
        }

        if appDelegate.isLogged()
        {
            followButton.hidden = false
            let follow = data["following"] as! Bool
            followButton.selected = follow
        }
        else
        {
            followButton.hidden = true
        }
    }

    @IBAction func addFollowing(sender: AnyObject)
    {
        let rs = appDelegate.contactManager.addFollowing(userName)

        if self.verifyResultset(rs)
        {
            followButton.selected = true
        }
    }
}
